$(document).ready(function(){
		var goodsClassId = $("#goodsClassId").val();
		var url = appPath+"/goods/listByClass/"+goodsClassId;
		//初始化sortTab
		initSortTab(true);
		//图片懒加载
		imageLazyInit();
		$(".sort_a").click(function(){
			var col = $(this).attr("col");
			var orderType = $(this).attr("orderType");
			var goodsState = $("#goodsState").val();
			if("price2"==col){//只有价格正反序
				if(3==goodsState){
					col = "points";
				}
				orderType = orderType == "asc" ?"desc":"asc";
			}
			var classId = $("#goodsClassId").val();
			var sort = col+"_"+orderType;
			var keyWord = $("#keyWord").val();
		    var brandId =$("#brandId").val();
		    var typeClass =$("#typeClass").val();
		    var goodsState =$("#goodsState").val();
			//var sortUrl = appPath+'/goods/findPageResult.html?goodsClassId='+classId+'&sort='+sort+"&keyWord="+keyWord+"&brandId="+brandId+"&typeClass="+typeClass;
			//$(this).attr("href",sortUrl);
			//location.replace(sortUrl);
			getContent(classId, 0,sort,keyWord,brandId,typeClass,goodsState,true,true);
		});
		//表头fixed 定义top属性
		//$("#tab-title").css("top",$("#header_div").height());
		$("#header_space_div").height($("#header_div").height());
		//下拉刷新
		$('#tab-title').height($(window).height()-$("#header_div").height());
		$("#content").pullToRefresh();
		var refreshing = false;
		$("#content").on("pull-to-refresh", function() {
			if($("#content").scrollTop()>0){
				return false;
			}
			refreshing = true;
			//var goodsId = $("#goods_id").val();
		    var classId = $("#goodsClassId").val();
		    var sort =$("#sort_input").val();
		    var keyWord =$("#keyWord").val();
		    var brandId =$("#brandId").val();
		    var typeClass =$("#typeClass").val();
		    var goodsState =$("#goodsState").val();
			getContent(classId, 0,sort,keyWord,brandId,typeClass,goodsState,true);
			$("#content").pullToRefreshDone();
		});
		//滚动加载
		$(document.body).infinite();
		var loading = false;  //状态标记
		var totalPage = $("#total_pages").val();
		$("#content").infinite(100).on("infinite", function() {
			if(loading) 
				return false;
			loading = true;
			var temFlag = true;
		  	if(refreshing)
		  		temFlag = false;
		    var page = $("#page").val();
		    if(page==null||page==""){
		    	page=0;
		    }
		    if(parseInt(page)+1==totalPage) 
		    	temFlag = false;
		    if(!temFlag){
		    	loading = false;
		    	return false;
		    }
		  	//alert("page>>>"+page);
		  	$("#loading-div").show();
		    //$("#list").append("<p> 我是新加载的内容 </p>");
		    var sort =$("#sort_input").val();
		    var classId = $("#goodsClassId").val();
		    var keyWord = $("#keyWord").val();
		    var brandId =$("#brandId").val();
		    var typeClass =$("#typeClass").val();
		    var goodsState =$("#goodsState").val();
		    $("#loading-div").show();
		    getContent(classId, parseInt(page)+1, sort,keyWord,brandId,typeClass,goodsState);
		});
		$("#search_cancel").click(function(){
			cancleSearch();
		});
	function getContent(goodsClassId,page,sort,keyWord,brandId,typeClass,goodsState,isRefresh,sortFlag){
		//if(isRefresh){
		var emptyListFlag = $("#emptyListFlag").val();
		if("1"==emptyListFlag){
			return;
		}
		$.showLoading();
		//}
		$.ajax({
			type: "GET",
			url : appPath+'/goods/findPageResult.html',
			dataType : 'html',
			data : {
				"goodsClassId" : goodsClassId,
				"page" : parseInt(page),
				"sort" : sort,
				"keyWord" : keyWord,
				"brandId":brandId,
				"typeClass":typeClass,
				"goodsState":goodsState
			},
			success : function(data) {
				$("#page").val(parseInt(page));
				var div =$(data).find("#goods_list");
				var appendHtml = $(div[0]).html();
				$.hideLoading();
				if(isRefresh){
					$("#goods_list").html(appendHtml);  
					refreshing = false;
				}else{
					$("#goods_list").append(appendHtml);  
					$("#loading-div").hide();
					$('#content').trigger('pull-to-refresh');
				}
				if(sortFlag){
					$("#sort_input").val(sort);
					initSortTab(false);
				}
				//图片懒加载
				imageLazyInit();
				loading = false;
			},error:function(msg){
				loading = false;
				$.hideLoading();
				$("#loading-div").hide();
				$.alert("系统异常！");
			}
		});
	}
	
	function formatNumWith2DecimalPlaces(num){
		var remainder = parseInt(num)%100;
		var str = num+"";
		str = str.substr(0,str.length-2);
		var remainderStr = remainder==0?".00":(remainder+"").length==1?".0"+remainder:"."+remainder;
		return str+remainderStr;
	}
});	

function imageLazyInit(){
	$("img.lazy-img").lazyload({
	    skip_invisible : true,
	    container: $("#goods_list")
	});
}
/**
 * 显示搜索框
 */
function showSearchBar(){
	$("#search_div").popup();
	$("#search_text").click();
}
function cancleSearch(){
	//$("#search_div").closePopup();
	$("#search_div").hide();
}
//数据类型 1:单品；2：列表页(根据分类)； 3:分类页；4：新闻列表页  ；5：新闻页 ；6：商品类型；7：品牌分类 ；9：关键字
function toLink(dataType, dataId) {
	var skuBaseUrl = "/goods/{goodsId}.htm";
	var goodsClassifiedBaseUrl = "/goods/list.htm?goodsClassId={goodsClassId}";
	//var categoryBaseUrl = "/goodsClass/{selectedClassId}.htm";
	var newsClassBaseUrl = "/article/article.htm?articleClassId={articleClassId}";
	var newsDetailBaseUrl = "/article/detail/{newsId}.htm";
	//6：商品类型
	var listWithTypeUrl = "/goods/list.htm?typeClass={typeClass}";
	//7：品牌
	var listWithBrandUrl = "/goods/list.htm?brandId={brandId}&goodsClassId={goodsClassId}";
	//9：关键字
	var listWithKeyWordUrl = "/goods/list.htm?keyWord={keyWord}";
	var url = null;
	switch (dataType) {
	case 1:
		url = skuBaseUrl.replace("{goodsId}", dataId);
		break;
	case 2:
		url = goodsClassifiedBaseUrl.replace("{goodsClassId}", dataId);
		break;
	case 3:
		url = categoryBaseUrl.replace("{selectedClassId}", dataId);
		break;
	case 4:
		url = newsClassBaseUrl.replace("{articleClassId}", dataId);
		break;
	case 5:
		url = newsDetailBaseUrl.replace("{newsId}", dataId);
		break;
	case 6:
		url = listWithTypeUrl.replace("{typeClass}", dataId);
		break;
	case 7:
		url = listWithBrandUrl.replace("{brandId}", dataId);
		if(indexClassDataId){
			url = url.replace("{goodsClassId}", indexClassDataId);
		}else{
			url = url.replace("{goodsClassId}", "");
		}
		break;
	case 9:
		url = listWithKeyWordUrl.replace("{keyWord}", dataId);
		break;
	default:
		url = "/index.html";
	break;
	}
	gotoHtml(url);
}
/**
 * 记录现场
 */
function recordScene(){
	var bodyContent = $("body").html();
	var contentScrollTop= $("#content").scrollTop();
	try{
		//记录内容
		sessionStorage.setItem("goodsListContent",bodyContent);
		//记录滚动
		sessionStorage.setItem("goodsListScrollTop",contentScrollTop);
		//记录返回flag
		sessionStorage.setItem("sceneFlag","1");
	}catch(e){
		
	}
}
/**
 * 还原现场
 */
function revertScene(){
	var sceneFlag = 0;
	//var detailBackFlag = 0;
	var notDetailBackFlag = 0;
	try{
		sceneFlag = sessionStorage.getItem("sceneFlag");
		//detailBackFlag = sessionStorage.getItem("detailBackFlag");
		notDetailBackFlag = sessionStorage.getItem("notDetailBackFlag");
	}catch(e){
		
	}
	//alert(sceneFlag +"  "+notDetailBackFlag);
	if("1"==sceneFlag&&"1"!=notDetailBackFlag){
		var bodyContent = sessionStorage.getItem("goodsListContent");
		var contentScrollTop= sessionStorage.getItem("goodsListScrollTop");
		$("body").html(bodyContent);
		$("#content").animate({scrollTop: contentScrollTop}, 0);
	}
	try{
		sessionStorage.removeItem("sceneFlag");
		//sessionStorage.removeItem("detailBackFlag");
		sessionStorage.removeItem("notDetailBackFlag");
	}catch(e){
		
	}
}

function initSortTab(isLoadInit){
	var sortParam = $("#sort_input").val();
	if(sortParam){
		var tem = sortParam.split("_");
		$(".sort_a").removeClass("weui_bar_item_on");
		$(".sort_a[col='"+tem[0]+"']").addClass("weui_bar_item_on");
		$(".sort_a i").removeClass();
		if("price2"==tem[0]||"points"==tem[0]){//只有价格正反序
			$(".sort_a[col='price2']").attr("orderType",tem[1]);
			$(".sort_a i").addClass(tem[1]);
		}
	}
	if(!isLoadInit){
		$('#content').animate({scrollTop:0},0);
	}
}
